﻿using System;
using System.Linq;
using HDTuyenChon.AppCode;
using System.Collections.Generic;

namespace HDTuyenChon.BL
{
	public class PhimBL
	{
		/// <summary>
		/// Tao ma phim
		/// </summary>
		/// <returns></returns>
		public string CreateMaPhim()
		{
			string msPhim = "";
			try
			{
				using (HDFilmDataContext db = new HDFilmDataContext())
				{
					var listPhim = (from p in db.Phims
									orderby p.ID descending
									select p).ToList();
					if (listPhim != null && listPhim.Count > 0)
					{
						Phim p = listPhim[0];
						msPhim = String.Format("{0:PH00000}", p.ID + 1);
					}
					else
					{
						msPhim = "PH00001";
					}
				}
			}
			catch (Exception exp)
			{
				throw new Exception("PhimBL.cs - createMaPhim - " + exp.Message.ToString(), exp);
			}

			return msPhim;
		}

		/// <summary>
		/// Insert phim
		/// </summary>
		/// <param name="phim"></param>
		/// <param name="fTaiKhoan"></param>
		/// <returns></returns>
		public int Insert(Phim phim, TaiKhoan fTaiKhoan)
		{
			try
			{
				using (HDFilmDataContext db = new HDFilmDataContext())
				{
					phim.NgayInsert = DateTime.Now;
					phim.NgayUpdate = DateTime.Now;
					phim.NguoiInsert = fTaiKhoan.UserName;
					phim.NguoiUpdate = fTaiKhoan.UserName;

					db.Phims.InsertOnSubmit(phim);
					db.SubmitChanges();
				}
			}
			catch (Exception exp)
			{
				throw new Exception("PhimBL.cs - insert - " + exp.Message.ToString(), exp);
			}

			return 1;
		}

		/// <summary>
		/// Lay danh sach phim tu loai phim
		/// </summary>
		/// <param name="loai"></param>
		/// <returns></returns>
		public List<Phim> GetListPhimByLoaiPhim(string loai)
		{
			List<Phim> phims = null;
			try
			{
				using (AppCode.HDFilmDataContext db = new AppCode.HDFilmDataContext())
				{
					if (loai == null)
					{
						phims = (from p in db.Phims
								orderby p.NgayUpdate descending
								 select p).ToList<Phim>();
					}
					else
					{
						loai = loai.Trim();

						//La So
						if (Utilities.IsNumber(loai))
						{
							phims = (from p in db.Phims
									where p.LoaiPhim == loai
									orderby p.NgayUpdate descending
									select p).ToList<Phim>();
						}
						else//Khong phai la so
						{
							switch (loai)
							{
								case "moi":
									phims = (from p in db.Phims
											orderby p.NgayUpdate
											select p).ToList<Phim>();
									break;
								case "xemNhieu":
									//So lan click vao link down + so lan copy phim
									phims = (from p in db.Phims
											orderby p.NgayUpdate descending
											 select p).ToList<Phim>();
									break;
								case "rap":
									phims = (from p in db.Phims
											where p.ChieuRap == true
											orderby p.NgayUpdate descending
											select p).ToList<Phim>();
									break;
								case "le":	
									var q = from p in db.Mstitemnames where p.ItemID == 4
											 && !p.ItemName.Contains("Phim Bộ") && !p.ItemName.Contains("Khác") 
	                                         select p.ItemValue;
									phims = (from ph in db.Phims where q.Contains(ph.LoaiPhim)
											select ph).ToList<Phim>();
	
									break;
								case "phimBo":
									var q1 = from p in db.Mstitemnames where p.ItemID == 4
											 && p.ItemName.Contains("Phim Bộ")
	                                         select p.ItemValue;
									phims = (from ph in db.Phims where q1.Contains(ph.LoaiPhim)
											select ph).ToList<Phim>();
									break;
								default:
									phims = (from p in db.Phims
											orderby p.NgayUpdate descending
											select p).ToList<Phim>();
									break;
							}
						}

					}
				}
			}
			catch (Exception exp)
			{
				throw new Exception("PhimBL.cs - GetListPhimByLoaiPhim - " + exp.Message.ToString(), exp);
			}
		
			return phims;
		}

		/// <summary>
		/// Get block title
		/// </summary>
		/// <param name="loai"></param>
		/// <returns></returns>
		public string GetBlockTitle(string loai)
		{
			if (loai == null)
			{
				return "Phim";
			}
			string title = "";
			try
			{
				using (HDFilmDataContext db = new HDFilmDataContext())
				{
					if (Utilities.IsNumber(loai))
					{
						var loaiPhim = (from p in db.Mstitemnames
									   where p.ItemID == 4 && p.ItemValue == loai
									   select p).SingleOrDefault();
						if (loaiPhim != null)
						{
							title = ((Mstitemname)loaiPhim).ItemName;
						}
					}
					else
					{
						switch (loai)
						{
							case "moi":
								title = "Phim Mới";
								break;
							case "xemNhieu":
								title = "Phim Xem Nhiều";
								break;
							case "rap":
								title = "Phim Chiếu Rạp";
								break;
							case "le":
								title = "Phim Lẻ";
								break;
							case "phimBo":
								title = "Phim Bộ";
								break;
							default:
								title = "Phim";
								break;
						}
					}
				}

			}
			catch (Exception exp)
			{
				throw new Exception("PhimBL.cs - GetBlockTitle - " + exp.Message.ToString(), exp);
			}

			return title;
		}
	}
}